<?php

namespace app\modules\setting\controllers\null;

use Yii;
use app\modules\setting\models\null\EtcOfficeTiktokAccount;
use app\modules\setting\configs\null\TiktokConfig;
use app\modules\setting\models\null\EtcOfficeTiktokAccountSearch;
use sookie\react\AdminController;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\base\Exception;
use sookie\excel\SaveExcel;
use sookie\db\DBHandler;


/**
 * TiktokController implements the CRUD actions for EtcOfficeTiktokAccount model.
 */
class TiktokController extends AdminController
{
    /**
    * @return array
    * 列表数据
    */
    public function actionIndex()
    {
        try {
            $searchModel = new EtcOfficeTiktokAccountSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            return ArrayHelper::merge(["data" =>$dataProvider->getModels(), "pagination" => $dataProvider->getPagination()],TiktokConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
    * @param $id
    * @return array
    * 详情接口
    */
    public function actionView($id)
    {
        try {
            return ArrayHelper::merge(["data" => EtcOfficeTiktokAccount::findOne($id)],TiktokConfig::configView());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
    * @return array
    * 新建接口
    */
    public function actionCreate()
    {
        try {
            if (Yii::$app->request->isPost){

                $model = new EtcOfficeTiktokAccount();

                if ($model->load(Yii::$app->request->post(),"") && DBHandler::adjustFieldLength(EtcOfficeTiktokAccount::tableName(),Yii::$app->request->post()) && $model->save()) {
                    return ["msg" => "添加成功"];
                }else{
                    return ["ok" => false,"msg" => $model->getFirstErrorStr(),"code" => 401];
                }
            }
            return TiktokConfig::configCreate();
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
    * @param $id
    * @return array
    * 修改接口
    */
    public function actionUpdate($id)
    {
        try {

            $model = $this->findModel($id);

            if (Yii::$app->request->isPost){

                if ($model->load(Yii::$app->request->post(),"") && DBHandler::adjustFieldLength(EtcOfficeTiktokAccount::tableName(),Yii::$app->request->post()) && $model->save()) {
                    return ["msg" => "修改成功"];
                }else{
                    return ["ok" => false,"msg" => $model->getFirstErrorStr(),"code" => 401];
                }
            }
            return TiktokConfig::configUpdate($model);
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
    * @param $id
    * @return array
    * 删除接口
    */
    public function actionDelete($id)
    {
        try {
            EtcOfficeTiktokAccount::deleteAll(["id" => $id]);

            return ["msg" => "删除成功"];

        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }


    protected function findModel($id)
    {
        if (($model = EtcOfficeTiktokAccount::findOne($id)) !== null) {
            return $model;
        }

        throw new Exception('不存在的记录');
    }




}

